package com.xiaobai.frame.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xiaobai.common.utils.TokenUtil;
import com.xiaobai.frame.entity.VO.userVO.login.LoginVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

import static com.xiaobai.common.utils.TokenUtil.ACCESS_EXPIRE;
import static com.xiaobai.common.utils.TokenUtil.REFRESH_EXPIRE;
import static com.xiaobai.frame.utils.RedisKey.REFRESH_TOKEN;

@Component
@AllArgsConstructor
public class TokenStorageUtil {

    private final RedisTool redisTool;

    public LoginVO redisSaveToken(JSONObject userInfo) {
        String refreshToken = TokenUtil.generateRefreshToken(JSON.parseObject(JSON.toJSONString(userInfo)));
        redisTool.setCacheObject(REFRESH_TOKEN + userInfo.get("id"), refreshToken, REFRESH_EXPIRE, TimeUnit.SECONDS);
        return new LoginVO(TokenUtil.generateAccessToken(JSON.parseObject(JSON.toJSONString(userInfo))),
            refreshToken, ACCESS_EXPIRE);
    }


}
